package com.newworld.goldcard.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.newworld.goldcard.R;
import com.newworld.goldcard.view.widget.SwipeBackLayout;

public abstract class BaseActivity<B extends ViewDataBinding> extends AppCompatActivity {
    protected B mBinding;
    protected Toolbar mToolbar;
    protected boolean isSwipe = true;//默认滚动关闭页面

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = getLayoutInflater().inflate(getLayoutId(), null, false);
        mBinding = DataBindingUtil.bind(view);
        setMyContentView(view);
        initToolbar();
        initView();
    }

    public void setMyContentView(View view) {
        setContentView(isSwipe ? getContainer(view) : view);
    }

    private View getContainer(View view) {
        view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.alpha_white));
        View container = getLayoutInflater().inflate(R.layout.activity_base, null, false);
        SwipeBackLayout swipeBackLayout = (SwipeBackLayout) container.findViewById(R.id.swipeBackLayout);
        swipeBackLayout.setDragEdge(SwipeBackLayout.DragEdge.LEFT);
        View ivShadow = container.findViewById(R.id.iv_shadow);
        swipeBackLayout.addView(view);
        swipeBackLayout.setOnSwipeBackListener((fa, fs) -> ivShadow.setAlpha(1 - fs));
        return container;
    }

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    abstract protected int getLayoutId();

    abstract protected void initView();

}
